package cn.dong.vod.controller.admin;

import cn.dong.api.domain.CommonResult;
import cn.dong.vod.service.impl.AdminVideoServiceImpl;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/vod")
@Slf4j
public class AdminVideoController {

    @Autowired
    private AdminVideoServiceImpl adminVideoService;

    /**
     * 上传视频
     * @return
     */
    @PostMapping("/upload")
    public CommonResult<String> upload(MultipartFile file){
        String id = null;
        try {
            id = adminVideoService.upload(file.getOriginalFilename(), file.getInputStream());
        } catch (IOException e) {
            return CommonResult.error().message(e.getMessage());
        }
        return CommonResult.ok().data(id).message("上传视频成功！");
    }


    /**
     * 删除视频
     * @return
     */
    @GetMapping("/delete/{videoId}")
    public CommonResult delete(@PathVariable("videoId") String videoId){

        try {
            CommonResult.whether(adminVideoService.delete(videoId));
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return CommonResult.ok();
    }


    /**
     * 批量删除视频
     * @return
     */
    @PostMapping("/deleteByIds")
    public CommonResult deleteByIds(@RequestBody List<String> videoIds){
        try {
            adminVideoService.deleteByIds(videoIds);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return CommonResult.ok();
    }


    /**
     * 根据视频id获取视频凭证
     * @param id
     * @return
     */
    @PostMapping("/getPlayAuth/{id}")
    public CommonResult getPlayAuth(@PathVariable String id){
        String auth = null;
        try {
            auth = adminVideoService.getPlayAuth(id);
        } catch (ClientException e) {
            log.error(e.getMessage());
            return CommonResult.error().message("获取视频播放地址失败，请稍后重试！");
        }
        return CommonResult.ok().data(auth);
    }
}
